/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.CRM.entity.CustomerExecutiveEntity;
import Merlion.CRM.bean.SalesLeadModuleRemote;
import Merlion.CRM.bean.SalesOrderProcessingModuleRemote;
import Merlion.CRM.entity.BillingAddressEntity;
import Merlion.CRM.entity.ShippingAddressEntity;
import Merlion.userManagement.entity.SystemUserEntity;
import Merlion.util.state.ClientState;
import Merlion.util.helper.ClientsAnalysis;
import Merlion.util.state.AddressState;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;


@ManagedBean(name = "CustomerManagedBean")
@ViewScoped
public class CustomerManagedBean implements Serializable {

  
    //BILLING ADDRESS
    private String billingStreet;
    private String billingCode;
    private String billingCountry;
    private String billingCity;
    private String billingState;
    //SHIPPING ADDRESS
    private String shippingStreet;
    private String shippingCode;
    private String shippingCountry;
    private String shippingCity;
    private String shippingState;
    private ClientState newCustomer = new ClientState();
    private List<ClientState> customers;//return list of customers
    private ClientState[] selectedCustomers;
    private boolean disable;
    private ClientState selectedCustomer;
    private List<SystemUserEntity> custExList;
    private SystemUserEntity custExecutive = new SystemUserEntity();
    private ClientState selectedViewCustomer = new ClientState();
    private boolean editMode;
    private ClientState selectedEditCustomer;
    private String message;
    private boolean duplicate;
    private List<ClientsAnalysis> customerList;
    private List<ClientsAnalysis> clientList;
    private String primaryShippingAddress;
    private String primaryBillingAddress;
    private ClientState selectedViewCustomer2;
    private Long companyId;
    private String addressType;
    private Long companyId2;
    private List<AddressState> addressList;
    private AddressState selectedAddress;
    private AddressState selectedPrimary;
    @EJB
    private SalesLeadModuleRemote smb;
    @EJB
    private SalesOrderProcessingModuleRemote sop;

    public CustomerManagedBean() {
       
    }

    public List<ClientsAnalysis> getClientList() {
        if (clientList == null) {
            clientList = smb.getClientList();
        }

        return clientList;
    }

    public void setClientList(List<ClientsAnalysis> clientList) {
        this.clientList = clientList;
    }

    public ClientState getSelectedViewCustomer2() {
        return selectedViewCustomer2;
    }

    public void setSelectedViewCustomer2(ClientState selectedViewCustomer2) {
        this.selectedViewCustomer2 = selectedViewCustomer2;
    }

    public List<ClientsAnalysis> getCustomerList() {
        if (customerList == null) {
            customerList = smb.getCustomerList();
        }
        return customerList;
    }

    public void setCustomerList(List<ClientsAnalysis> customerList) {
        this.customerList = customerList;
    }

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (customers == null) {
            customers = smb.viewCustomers();
        }
    }

    public boolean isDuplicate() {
        String dupString = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("duplicate");
        if (dupString.equals("true")) {
            duplicate = true;
        } else if (dupString.equals("false")) {
            duplicate = false;
        }

        return duplicate;
    }

    public void setDuplicate(boolean duplicate) {
        this.duplicate = duplicate;
    }

    public boolean isDisable() {
        return disable;
    }

    public void setDisable(boolean disable) {
        this.disable = disable;
    }

    public ClientState getNewCustomer() {
        return newCustomer;
    }

    public void setNewCustomer(ClientState newCustomer) {
        this.newCustomer = newCustomer;
    }

    public ClientState getSelectedEditCustomer() {
        return selectedEditCustomer;
    }

    public void setSelectedEditCustomer(ClientState selectedEditCustomer) {
        this.selectedEditCustomer = selectedEditCustomer;
    }

    public ClientState getSelectedViewCustomer() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (selectedViewCustomer == null) {
            selectedViewCustomer = new ClientState();
        }
        return selectedViewCustomer;
    }

    public void setSelectedViewCustomer(ClientState selectedViewCustomer) {
        System.out.println("Set Selected View Customer");
        this.selectedViewCustomer = selectedViewCustomer;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }


    public SystemUserEntity getCustExecutive() {
        return custExecutive;
    }

    public void setCustExecutive(SystemUserEntity custExecutive) {
        this.custExecutive = custExecutive;
    }

    public List<SystemUserEntity> getCustExList() {
        if (custExList == null) {
            custExList = smb.getAllCustomerExecutives();
        }
        return this.custExList;

    }

    public void setCustExList(List< SystemUserEntity> custExList) {
        this.custExList = custExList;
    }

    public void addCustomer(ActionEvent event) {
        System.out.println(":::WEB::: Add new customer");

        String name = newCustomer.getName();
        String industry = newCustomer.getIndustry();
        double annualRevenue = newCustomer.getAnnualRevenue();
        int numOfEmployees = newCustomer.getNumOfEmployees();
        String addressState = newCustomer.getAddressState();
        String faxNum = newCustomer.getFaxNum();
        String website = newCustomer.getWebsite();
        double rating = newCustomer.getRating();
        String address = newCustomer.getAddress();
        String contactNum = newCustomer.getContactNum();
        String country = newCustomer.getCountry();
        String city = newCustomer.getCity();
        String postalCode = newCustomer.getPostalCode();
        String emailAddress = newCustomer.getEmailAddress();
        String businessType = newCustomer.getBusinessType();
        String clientType = newCustomer.getClientType();
        String description = newCustomer.getDescription();
        String tickerSymbol = newCustomer.getTickerSymbol();
        String ownership = newCustomer.getOwnership();
        String sicCode = newCustomer.getSicCode();
        String custExId = newCustomer.getCustExId();
        String companyRegId = newCustomer.getCompanyRegId();


        Long returnCustomerId = smb.addCustomer(name, companyRegId, industry, annualRevenue, numOfEmployees,
                contactNum, faxNum, website, rating,
                address, addressState, country, city, postalCode,
                emailAddress, businessType, description,
                custExId, ownership, tickerSymbol, sicCode);


        if (returnCustomerId != 0) {
            System.out.println(":::WEB::: Create billing and shipping address");
            Long[] returnId = smb.createBillingShippingAddress(returnCustomerId, billingStreet, billingCity, billingState, billingCode, billingCountry,
                    shippingStreet, shippingCity, shippingState, shippingCode, shippingCountry);

            Long billingId = returnId[0];
            Long shippingId = returnId[1];

            System.out.println("Setting primary shipping and billing address");
            smb.setPrimaryBillingAddress(billingId, returnCustomerId);
            smb.setPrimaryShippingAddress(shippingId, returnCustomerId);


            System.out.println(":::WEB::: Customer ID is : " + returnCustomerId);
            String successMessage = "\t\t\tYou have sucessfully added a new customer!";
            setDuplicate(false);
            setMessage(successMessage);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("duplicate", "false");
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("message", successMessage);
        } else {
            setDuplicate(true);
            String failMessage = "\t\t\tDuplicate customer data found in the database!" + "\n"
                    + "\t\t\tCustomer is not added to the system.";
            setMessage(failMessage);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("duplicate", "true");
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("message", failMessage);
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("createNewCustomerStatus.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public String getMessage() {
        if (message == null) {
            message = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("message");
        }
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void editCustomer(ActionEvent event) {
        try {

            FacesContext.getCurrentInstance().getExternalContext().redirect("editCustomer.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public List<ClientState> getCustomers() {
   
        return this.customers;
    }

    public ClientState getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(ClientState selectedCustomer) {
        System.out.println(":::WEB::: Set Selected Customer");
        this.selectedCustomer = selectedCustomer;

    }

    public void onEditRowCustomer(RowEditEvent event) {

        System.out.println(":::WEB::: On Edit Row");
        ClientState eClient = (ClientState) event.getObject();

        if (eClient != null) {
            System.out.println(":::WEB::: Edited client not null");
            String eName = eClient.getName();
            String eIndustry = eClient.getIndustry();
            double eAnnualRev = eClient.getAnnualRevenue();
            int eNumOfEmployees = eClient.getNumOfEmployees();
            String eFaxNum = eClient.getFaxNum();
            String eWebsite = eClient.getWebsite();
            double eRating = eClient.getRating();
            String eAddress = eClient.getAddress();
            String eAddressState = eClient.getAddressState();
            String eContact = eClient.getContactNum();
            String eCountry = eClient.getCountry();
            String eCity = eClient.getCity();
            String ePostalCode = eClient.getPostalCode();
            String eEmailAddress = eClient.getEmailAddress();
            String eType = eClient.getBusinessType();
            String eDescription = eClient.getDescription();
            String eCustExId = eClient.getCustExId();
            String eOwnership = eClient.getOwnership();
            String eTickerSymbol = eClient.getTickerSymbol();
            String eSicCode = eClient.getTickerSymbol();
            System.out.println(":::WEB::: Cust Ex Id is :" + eCustExId);
            Long clientId = eClient.getId();
            System.out.println(":::WEB::: Calling Edit Customer");
            smb.editCustomer(clientId, eName, eIndustry, eAnnualRev, eNumOfEmployees, eContact,
                    eFaxNum, eWebsite, eRating, eAddress, eAddressState, eCountry, eCity,
                    ePostalCode, eEmailAddress, eType, eDescription, eCustExId,
                    eOwnership, eTickerSymbol, eSicCode);

            //EDIT BILLING ADDRESS
            System.out.println(":::WEB::: Calling Edit Billing Address");
            String eBStreet = eClient.getBillingStreet();
            String eBCity = eClient.getBillingCity();
            String eBCountry = eClient.getBillingCountry();
            String eBState = eClient.getBillingState();
            String eBCode = eClient.getBillingCode();
            smb.editBillingAddress(clientId, eBStreet, eBCity, eBState, eBCode, eBCountry);

            //EDIT SHIPPING ADDRESS
            System.out.println(":::WEB::: Calling Edit Shipping Address");
            String eSStreet = eClient.getShippingStreet();
            String eSCity = eClient.getShippingCity();
            String eSCountry = eClient.getShippingCountry();
            String eSState = eClient.getShippingState();
            String eSCode = eClient.getShippingCode();
            smb.editShippingAddress(clientId, eSStreet, eCity, eSState, eSCode, eSCountry);

        } else {
            System.out.println(":::WEB::: Edited client is null");
        }

    }

    public void redirectToCustomers() {
        System.out.println("Redirected");
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void delete(ActionEvent Event) {

        System.out.println(":::WEB::: Delete Customer");

        if (selectedCustomer != null) {
            System.out.println(":::WEB::: Selected customer not null");
            Long customerId = selectedCustomer.getId();
            System.out.println(":::WEB::: Customer ID is : " + customerId);
            smb.deleteCustomer(customerId);
            System.out.println(":::WEB::: Deletion done");
        } else {
            System.out.println(":::WEB::: Selected customer is null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public ClientState[] getSelectedCustomers() {
        return selectedCustomers;
    }

    public void setSelectedCustomers(ClientState[] selectedCustomers) {
        System.out.println(":::WEB::: Set selected customers");
        this.selectedCustomers = selectedCustomers;
    }

    public void deleteCustomers() {
        System.out.println(":::WEB::: Delete Customers");

        if (selectedCustomers != null) {
            System.out.println(":::WEB::: If selected customers not null");
            for (int i = 0; i < selectedCustomers.length; i++) {
                System.out.println("selectedCustomers i: " + i + " - " + selectedCustomers[i].getId());
                smb.deleteCustomer(selectedCustomers[i].getId());
            }
        } else {
            System.out.println(":::WEB::: Selected customers are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void cancelDelete() {
        for (ClientState client : selectedCustomers) {
            client = null;
        }
        if (selectedCustomer == null) {
            System.out.println(":::WEB::: Deletion of customers cancelled");

        } else {
            System.out.println(":::WEB::: Deletion of customers are not cancelled");
        }

    }

    public void onRowSelect(SelectEvent event) {
        if (selectedCustomers == null) {
            disable = true;
        } else {
            if (selectedCustomers.length > 0) {
                disable = false;
            } else {
                disable = true;
            }
        }
    }

    public void onRowUnselect(UnselectEvent event) {
        if (selectedCustomers == null) {
            disable = true;
        } else {
            if (selectedCustomers.length > 0) {
                disable = false;
            } else {
                disable = true;
            }
        }
    }

 
    public String getBillingCountry() {
        return billingCountry;
    }

    public void setBillingCountry(String billingCountry) {
        if (billingCountry.equals("")) {
            billingCountry = "-";
        }
        this.billingCountry = billingCountry;
    }

    public String getBillingState() {
        return billingState;
    }

    public void setBillingState(String billingState) {
        if (billingState.equals("")) {
            billingState = "-";
        }
        this.billingState = billingState;
    }

    public String getBillingStreet() {
        return billingStreet;
    }

    public void setBillingStreet(String billingStreet) {
        if (billingStreet.equals("")) {
            billingStreet = "-";
        }
        this.billingStreet = billingStreet;
    }

    public String getBillingCity() {
        return billingCity;
    }

    public void setBillingCity(String billingCity) {
        if (billingCity.equals("")) {
            billingCity = "-";
        }
        this.billingCity = billingCity;
    }

    public String getBillingCode() {
        return billingCode;
    }

    public void setBillingCode(String billingCode) {
        if (billingCode.equals("")) {
            billingCode = "-";
        }
        this.billingCode = billingCode;
    }


    public String getShippingCity() {
        return shippingCity;
    }

    public void setShippingCity(String shippingCity) {
        if (shippingCity.equals("")) {
            shippingCity = "-";
        }
        this.shippingCity = shippingCity;
    }

    public String getShippingCode() {
        return shippingCode;
    }

    public void setShippingCode(String shippingCode) {
        if (shippingCode.equals("")) {
            shippingCode = "-";
        }
        this.shippingCode = shippingCode;
    }

    public String getShippingCountry() {
        return shippingCountry;
    }

    public void setShippingCountry(String shippingCountry) {
        if (shippingCountry.equals("")) {
            shippingCountry = "-";
        }
        this.shippingCountry = shippingCountry;
    }

    public String getShippingState() {
        return shippingState;
    }

    public void setShippingState(String shippingState) {
        if (shippingState.equals("")) {
            shippingState = "-";
        }
        this.shippingState = shippingState;
    }

    public String getShippingStreet() {
        return shippingStreet;
    }

    public void setShippingStreet(String shippingStreet) {
        if (shippingStreet.equals("")) {
            shippingStreet = "-";
        }
        this.shippingStreet = shippingStreet;
    }


    public void save(ActionEvent event) {
        System.out.println(":::WEB::: Save Edit customer");

        Long customerId = selectedViewCustomer.getId();
        String cName = selectedViewCustomer.getName();
        String cIndustry = selectedViewCustomer.getIndustry();
        double cAnnualRev = selectedViewCustomer.getAnnualRevenue();
        System.out.println("Annual Revenue is " + cAnnualRev);
        int cNumOfEmployees = selectedViewCustomer.getNumOfEmployees();
        String cAddressState = selectedViewCustomer.getAddressState();
        String cFaxNum = selectedViewCustomer.getFaxNum();
        String cWebsite = selectedViewCustomer.getWebsite();
        double cRating = selectedViewCustomer.getRating();
        String cContactNumber = selectedViewCustomer.getContactNum();
        String cAddress = selectedViewCustomer.getAddress();
        String cCountry = selectedViewCustomer.getCountry();
        String cCity = selectedViewCustomer.getCity();
        String cPostalCode = selectedViewCustomer.getPostalCode();
        String cEmailAddress = selectedViewCustomer.getEmailAddress();
        String cBusinessType = selectedViewCustomer.getBusinessType();
        String cDescription = selectedViewCustomer.getDescription();
        String cCustExId = selectedViewCustomer.getCustExId();
        String cOwnership = selectedViewCustomer.getOwnership();
        String cTickler = selectedViewCustomer.getTickerSymbol();
        String cSicCode = selectedViewCustomer.getSicCode();
        System.out.println("Client ID is :" + customerId + ".Client Name : " + cName);
        System.out.println(":::WEB::: Calling edit customer");
        smb.editCustomer(customerId, cName, cIndustry, cAnnualRev, cNumOfEmployees,
                cContactNumber, cFaxNum, cWebsite, cRating,
                cAddress, cAddressState, cCountry, cCity, cPostalCode, cEmailAddress,
                cBusinessType, cDescription, cCustExId, cOwnership, cTickler, cSicCode);

        //EDIT BILLING ADDRESS
        Long billingId = selectedViewCustomer.getBillingId();
        System.out.println(":::WEB::: Calling Edit Billing Address");
        String eBStreet = selectedViewCustomer.getBillingStreet();
        String eBCity = selectedViewCustomer.getBillingCity();
        String eBCountry = selectedViewCustomer.getBillingCountry();
        String eBState = selectedViewCustomer.getBillingState();
        String eBCode = selectedViewCustomer.getBillingCode();
        smb.editBillingAddress(billingId, eBStreet, eBCity, eBState, eBCode, eBCountry);

        //EDIT SHIPPING ADDRESS
        Long shippingId = selectedViewCustomer.getShippingId();
        System.out.println(":::WEB::: Calling Edit Shipping Address");
        String eSStreet = selectedViewCustomer.getShippingStreet();
        String eSCity = selectedViewCustomer.getShippingCity();
        String eSCountry = selectedViewCustomer.getShippingCountry();
        String eSState = selectedViewCustomer.getShippingState();
        String eSCode = selectedViewCustomer.getShippingCode();
        smb.editShippingAddress(shippingId, eSStreet, eSCity, eSState, eSCode, eSCountry);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public String getPrimaryBillingAddress() {
        return primaryBillingAddress;
    }

    public void setPrimaryBillingAddress(String primaryBillingAddress) {
        this.primaryBillingAddress = primaryBillingAddress;
    }

    public String getPrimaryShippingAddress() {
        return primaryShippingAddress;
    }

    public void setPrimaryShippingAddress(String primaryShippingAddress) {
        this.primaryShippingAddress = primaryShippingAddress;
    }

    public void handleStreet() {
        if (newCustomer.getAddress() != null) {
            setShippingStreet(newCustomer.getAddress());
            setBillingStreet(newCustomer.getAddress());
        }
    }

    public void handleState() {
        if (newCustomer.getAddressState() != null) {
            setShippingState(newCustomer.getAddressState());
            setBillingState(newCustomer.getAddressState());
        }

    }

    public void handleCity() {
        if (newCustomer.getCity() != null) {
            setShippingCity(newCustomer.getCity());
            setBillingCity(newCustomer.getCity());
        }
    }

    public void handleCountry() {
        if (newCustomer.getCountry() != null) {
            setShippingCountry(newCustomer.getCountry());
            setBillingCountry(newCustomer.getCountry());
        }
    }

    public void handleCode() {
        if (newCustomer.getPostalCode() != null) {
            setShippingCode(newCustomer.getPostalCode());
            setBillingCode(newCustomer.getPostalCode());
        }
    }

    public void addShippingAddress(ActionEvent event) {
        System.out.println("WEB:Add shipping address");
        ShippingAddressEntity sa = smb.addShippingAddress(shippingStreet, shippingCity, shippingState, shippingCode, shippingCountry);
        Long saId = sa.getId();
        System.out.println("Associate with customers-s");
        smb.associateShippingAddressWithCustomer(companyId2, saId);
        if (primaryShippingAddress.equals("Yes")) {
            smb.setPrimaryShippingAddress(saId, companyId2);
        }
        System.out.println("Shipping address id is : " + saId);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void addBillingAddress(ActionEvent event) {
        System.out.println("WEB:Add billing address");
        BillingAddressEntity ba = smb.addBillingAddress(billingStreet, billingCity, billingState, billingCode, billingCountry);
        Long baId = ba.getId();
        System.out.println("Associate with customers-b");
        smb.associateBillingAddressWithCustomer(companyId, baId);
        if (primaryBillingAddress.equals("Yes")) {
            smb.setPrimaryBillingAddress(baId, companyId);
        }
        System.out.println("Billing address id is " + baId);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public Long getCompanyId() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public Long getCompanyId2() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        return companyId2;
    }

    public void setCompanyId2(Long companyId2) {
        this.companyId2 = companyId2;
    }

    public String getAddressType() {
        return addressType;
    }

    public void setAddressType(String addressType) {
        this.addressType = addressType;
    }

    public void handleAddressTypeChange() {
        if (companyId != null) {

            System.out.println("Company id not null");

            ClientState clState = smb.searchClient(companyId);


            System.out.println("Set billing and shipping address");

            List<AddressState> addrList = clState.getAddressList();

            List<AddressState> billingAddressList = new ArrayList<AddressState>();
            List<AddressState> shippingAddressList = new ArrayList<AddressState>();

            for (Object o : addrList) {
                AddressState as = (AddressState) o;
                if (as.getType().equals("B")) {
                    billingAddressList.add(as);
                } else if (as.getType().equals("S")) {
                    shippingAddressList.add(as);
                }
            }
            if (addressType.equals("B")) {
                this.setAddressList(billingAddressList);
            } else {
                this.setAddressList(shippingAddressList);
            }

        }
    }

    public List<AddressState> getAddressList() {

        return addressList;
    }

    public void setAddressList(List<AddressState> addressList) {
        this.addressList = addressList;
    }

    public AddressState getSelectedAddress() {
        return selectedAddress;
    }

    public void setSelectedAddress(AddressState selectedAddress) {
        this.selectedAddress = selectedAddress;
    }

    public AddressState getSelectedPrimary() {
        return selectedPrimary;
    }

    public void setSelectedPrimary(AddressState selectedPrimary) {
        System.out.println("SET SELECTED PRIMARY");
        if (selectedPrimary != null) {
            if (selectedPrimary.getType().equals("B")) {
                smb.setPrimaryBillingAddress(selectedPrimary.getId(), companyId);
            } else {
                smb.setPrimaryShippingAddress(selectedPrimary.getId(), companyId);
            }
        }
        this.selectedPrimary = selectedPrimary;
    }

    public void onEditRowAddress(RowEditEvent event) {

        System.out.println(":::WEB::: On Edit Row");
        AddressState eAddress = (AddressState) event.getObject();

        System.out.println(":::WEB::: Calling Edit Address");
        String eBStreet = eAddress.getStreet();
        String eBCity = eAddress.getCity();
        String eBCountry = eAddress.getCountry();
        String eBState = eAddress.getState();
        String eBCode = eAddress.getCode();
        Long eId = eAddress.getId();

        if (eAddress.getType().equals("B")) {
            smb.editBillingAddress(eId, eBStreet, eBCity, eBState, eBCode, eBCountry);
        } else {
            smb.editShippingAddress(eId, eBStreet, eBCity, eBState, eBCode, eBCountry);
        }

    }

    public void deleteAddress(ActionEvent event) {

        if (selectedAddress != null) {
            if (!selectedAddress.isPrimaryAddress()) {
                if (selectedAddress.getType().equals("B")) {
                    smb.removeBillingAddressfromCustomerData(selectedAddress.getId(), companyId);
                } else {
                    smb.removeShippingAddressfromCustomerData(selectedAddress.getId(), companyId);
                }
                try {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("customers.xhtml");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            } else {
                System.out.println("Primary Address cannot be deleted!");
                FacesContext context = FacesContext.getCurrentInstance();

                context.addMessage(null, new FacesMessage("Deletion failed!", "Primary address cannot be deleted."));

            }
        }


    }

    public void setPrimaryAddress(ActionEvent event) {
        System.out.println("SETTING PRIMARY ADDRESS");

        ClientState clState = smb.searchClient(companyId);


        System.out.println("Set billing and shipping address (setPrimaryAddress)");

        List<AddressState> addrList = clState.getAddressList();

        List<AddressState> billingAddressList = new ArrayList<AddressState>();
        List<AddressState> shippingAddressList = new ArrayList<AddressState>();

        for (Object o : addrList) {
            AddressState as = (AddressState) o;
            if (as.getType().equals("B")) {
                billingAddressList.add(as);
            } else if (as.getType().equals("S")) {
                shippingAddressList.add(as);
            }
        }

        if (selectedPrimary != null) {
            System.out.println("selected primary not null");
            if (selectedPrimary.getType().equals("B")) {
                System.out.println("set primary billing address");
                smb.setPrimaryBillingAddress(selectedPrimary.getId(), companyId);
                addrList = clState.getAddressList();

                billingAddressList = new ArrayList<AddressState>();
                shippingAddressList = new ArrayList<AddressState>();

                for (Object o : addrList) {
                    AddressState as = (AddressState) o;
                    if (as.getType().equals("B")) {
                        billingAddressList.add(as);
                    } else if (as.getType().equals("S")) {
                        shippingAddressList.add(as);
                    }
                }
                this.setAddressList(billingAddressList);

            } else {
                System.out.println("set primary shipping address");
                smb.setPrimaryShippingAddress(selectedPrimary.getId(), companyId);

                addrList = clState.getAddressList();

                billingAddressList = new ArrayList<AddressState>();
                shippingAddressList = new ArrayList<AddressState>();

                for (Object o : addrList) {
                    AddressState as = (AddressState) o;
                    if (as.getType().equals("B")) {
                        billingAddressList.add(as);
                    } else if (as.getType().equals("S")) {
                        shippingAddressList.add(as);
                    }
                }
                this.setAddressList(shippingAddressList);
            }
        }
    }

    public void handlePrimaryAddressChange() {
        System.out.println("SETTING PRIMARY ADDRESS");

        if (selectedPrimary != null) {
            System.out.println("selected primary not null");
            if (selectedPrimary.getType().equals("B")) {
                smb.setPrimaryBillingAddress(selectedPrimary.getId(), companyId);
            } else {
                smb.setPrimaryShippingAddress(selectedPrimary.getId(), companyId);
            }
        }
    }
    

}
